package com.yongyun.reptile.dao;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.keix.common.utils.StringUtils;

/** 
* @author liyongkang
* @version 创建时间：2018年9月13日 下午12:45:05 
* 类说明 ：
*/

public class ArticleDao {
	
	private static final String INSERT_ARTICLE = "INSERT INTO article VALUES (?, ?, ?, ?)";
	
	public static void insertArticle(Connection conn, String uuid, String content, String comment, String oraginUrl) throws SQLException{
		PreparedStatement ps = conn.prepareStatement(INSERT_ARTICLE);
		try {
			ps.setString(1, uuid);
			try {
				if(!StringUtils.isEmail(content)){
					ps.setBytes(2, content.getBytes("UTF-8"));
				}else{
					ps.setBytes(2, null);
				}
				if(!StringUtils.isEmail(comment)){
					ps.setBytes(3, comment.getBytes("UTF-8"));
				}else{
					ps.setBytes(3, null);
				}
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			ps.setString(4, oraginUrl);
			ps.execute();
		} finally {
			ps.close();
		}
	}
	
	private static final String SELECT_ARTICLE = "select * from article where A_uuid = ? ";
	
	public static String selectArticle(Connection conn, String uuid) throws SQLException{
		String content = null;
		PreparedStatement ps = conn.prepareStatement(SELECT_ARTICLE);
		try {
			ps.setString(1, uuid);
			ResultSet rs = ps.executeQuery();
			try {
				while (rs.next()) {
					if(rs.getBytes(2)!=null){
						try {
							content = new String(rs.getBytes(2), "UTF-8");
						} catch (UnsupportedEncodingException e) {
							e.printStackTrace();
						}
					}
				}
			} finally {
				rs.close();
			}
		} finally {
			ps.close();
		}
		return content;
	}
	
	private static final String INSERT_NOURL = "INSERT INTO NOURL VALUES (?, ?)";
	
	public static void insertNoUrl(Connection conn, String uuid, String url) throws SQLException{
		PreparedStatement ps = conn.prepareStatement(INSERT_NOURL);
		try {
			ps.setString(1, uuid);
			ps.setString(2, url);
			ps.execute();
		} finally {
			ps.close();
		}
	}
}
